import re

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
import os
import urllib.request
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
print('文本获取：长裤输入0 七分裤输入1 五分裤输入2 短裤输入3 七分裤／九分裤输入4')
href = input()

if href == "0":
    ff = open('长裤链接.txt',  encoding='utf-8')
    content = ff.readlines()
    ff.close()
    KEYWORD = '长裤信息'
if href == "1":
    ff = open('七分裤链接.txt',  encoding='utf-8')
    content = ff.readlines()
    ff.close()
    KEYWORD = '七分裤信息'
if href == "2":
    ff = open('五分裤链接.txt',  encoding='utf-8')
    content = ff.readlines()
    ff.close()
    KEYWORD = '五分裤信息'
if href == "3":
    ff = open('短裤链接.txt',  encoding='utf-8')
    content = ff.readlines()
    ff.close()
    KEYWORD = '短裤信息'
if href == "4":
    ff = open('七分裤九分裤链接.txt',  encoding='utf-8')
    content = ff.readlines()
    ff.close()
    KEYWORD = '七分裤九分裤信息'
def search(url):
    print('正在搜索'+ KEYWORD + "文本链接")
    try:
        browser.get(url)
        total = wait.until(EC.presence_of_element_located((By.ID, 'J_AttrList')))
        all_links = browser.find_element_by_xpath("./*//ul[@id='J_AttrUL']").text
        print(all_links)
        with open(KEYWORD+'.txt', 'a', encoding='utf-8') as f:
            f.write(all_links+'\n')
        f.close()
        return total.text
    except Exception as e:
        print(e)
        search()


def main():
    for elem in content:
        elem = str(elem)
        print("当前链接",elem)
        try:
            search(elem)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    main()